package com.skyline.base.mapper;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.springframework.jdbc.core.RowMapper;

import com.skyline.base.model.Comment;
import com.skyline.base.type.CommentType;

/**
 * 评论的mapper
 * 
 * @author Jairus Chan
 * @version 0.1
 * */
public class CommentMapper implements RowMapper<Comment> {

	private static final CommentMapper SHORTMESSAGE_MAPPER = new CommentMapper(true);
	private static final CommentMapper MAPPER = new CommentMapper(false);

	private boolean isShortMessageMapper=false;

	public CommentMapper(boolean isShortMessageMapper) {
		// type=type.replaceAll("comment", "");
		this.isShortMessageMapper=isShortMessageMapper;
	}

	public static CommentMapper getMapper(String type) {
		type=type.toUpperCase();
		if (type != null && type.equals(CommentType.SHORTMESSAGE.toString()+"COMMENT")) {
			return SHORTMESSAGE_MAPPER;
		} else {
			return MAPPER;
		}
	}

	public Comment mapRow(ResultSet rs, int rowNum) throws SQLException {
		Comment c = new Comment();
		c.setId(rs.getLong("id"));
		c.setContent(rs.getString("content"));
		c.setCreateTime(rs.getTimestamp("createtime"));
		c.setCommenterId(rs.getLong("commenterId"));
		c.setCommenterNickname(rs.getString("commenterNickname"));
		c.setCommenterPortrait(rs.getString("commenterPortrait"));
		c.setRef(rs.getLong("ref"));
		if (this.isShortMessageMapper==true) {
			c.setResourceId(rs.getString("resourceId"));
		} else {
			c.setResourceId(rs.getLong("resourceId"));
		}
		c.setOwnerId(rs.getLong("ownerId"));
		c.setAgreeNum(rs.getInt("agreeNum"));
		c.setDisagreeNum(rs.getInt("disagreeNum"));
		return c;
	}
}
